<template>
  <div class="wrap">
    <div class="header">
      <div class="search">
        <user-account-check-box v-model="queryParams.accountIds" ></user-account-check-box>
        <div style="display: inline-block;" class="mgt20">
          <el-input size="small" style="width: 250px" v-model="queryParams.keyword" placeholder="搜索完整Listing ID或SKU"></el-input>
          <el-button size="small" type="primary" @click="changeOption">搜索</el-button>
        </div>
        <el-button>导出</el-button>
      </div>


    </div>
    <div class="content">
      <el-table
        v-loading="loading"
        :data="data"
        border
      >
        <el-table-column
          prop="image"
          label="封面"
          align="center"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="title"
          label="用户"
          align="left"
          min-width="250"
        >
        </el-table-column>
        <el-table-column
          prop="goodsNum"
          label="店铺"
          align="center"
          min-width="150"
        >
        </el-table-column>
        <el-table-column
          prop="num"
          label="标题"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="underWayNum"
          label="适用 Listing ID与SKU"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="usedNum"
          label="前台库存"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="usedNum"
          :label="getWareHouseName() + '库存'"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="usedNum"
          label="在途库存"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="sales"
          label="已销售"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="op"
          label="操作"
          align="center"
        >
          <template slot-scope="{row}">
            <el-button>修改库存</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-tips">
        <p>前台库存：美客多商品链接中显示的库存余量，通过美客多API获得</p>
        <p>美刻达库存：美刻达仓库中已确认入库的库存及其余量</p>
      </div>
      <div class="page-wrap">
        <el-pagination
          @current-change="getData"
          :current-page.sync="queryParams.page"
          :page-sizes="[10, 20, 50]"
          :page-size="queryParams.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </div>


  </div>
</template>

<script>

import {deleteStock, stockList, suitStockGoods} from "@/api/stock";
import {deepCopy} from "@/utils";
import {getGoodsList} from "@/api/goods";
import ExportButton from "@/views/order/components/ExportButton.vue";
import UserAccountCheckBox from "@/views/order/components/UserAccountCheckBox.vue";
import {getWareHouseName} from "@/utils/common";

export default {
  name: "stockList",
  components: {
    UserAccountCheckBox,

  },
  data(){
    return {
      loading: false,
      queryParams: {
        page: 1,
        pageSize: 10,
        keyword: '',

      },
      data:[],
      total: 0,
    }
  },
  created() {
    // this.setQueryParams()
    // this.getData();
  },
  methods:{
    getWareHouseName,
    // 获取表单数据
    getData(){
      this.loading = true
      stockList(this.queryParams)
        .then(res=>{
          this.loading = false
          this.data = deepCopy(res.data)
        })
    },
    // 搜索表格数据
    search(){
      this.page = 1;
      this.getData()
    },
    // 添加库存按钮事件
    addStock(){
      // 跳转到添加页面
      this.$router.push({
        name: 'addStock'
      })
    },
    // 编辑适用的商品
    suitGoods(row){
      this.selectGoods = [];
      this.selectGoods.push(...row.goods)
      this.suitData = [];
      this.rowId = row.id;
      this.suitParams.keyword = ''
      this.dialogSuit = true;
    },
    // 获取商品的列表
    getSuitData(){
      this.goodsTableLoading = true
      getGoodsList(this.suitParams)
        .then(res=>{
          this.goodsTableLoading = false
          const { data, info } = res
          this.suitData = deepCopy(data)

        })
    },
    // 移除选择的商品
    removeSelect(row){
      this.selectGoods = this.selectGoods.filter(item => item.id !== row.id)
    },
    // 已选择过的 不能再选择
    disAbleAdd(row){
      if(row.stockId > 0 && row.stockId !== this.rowId){
        return true;
      }

      let flag = this.selectGoods.find(item=>{
        if(item.id === row.id){
          return true;
        }
      })
      return !!flag;
    },
    // 添加商品
    addSelectGoods(row) {
      this.selectGoods.push(row)
    },
    deleteStock(row){
      this.$confirm('确定删除该库存吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(()=>{
          deleteStock(row.id)
            .then(res=>{
              this.$successMessage(res.message)
              this.getData()
            })
        })
        .catch(err => { console.error(err) })

    },
    //
    confirmSuit(){
      const postData = {
        id: this.rowId,
        suitGoods: deepCopy(this.selectGoods)
      }
      this.suitConfirmLoading = true;
      suitStockGoods(postData)
        .then(res=>{
          this.suitConfirmLoading = false;
          this.dialogSuit = false; // 关闭窗口
          this.$successMessage('操作成功');
          this.getData()
        })

    }
  }
}
</script>
<style lang="scss">
.goods-table{
  .el-table__expand-icon {
    float: left;
    line-height: 100px;
    height: 100px;
    font-size: 14px;
    position: absolute;
    top: 30px;
    left: 5px;
  }
}
</style>

<style scoped lang="less">
.wrap {
  padding: 30px 30px;
  .header{
    display: flex;
    justify-content: space-between;
  }
  .content{
    padding: 20px 0 0;
  }
}

</style>

<style lang="less">
.channel-form{
  &.el-form{
    .el-form-item__label{
      word-break: break-word;
    }
  }
}
.channel-box-card{
  .el-card__header{
    padding: 5px 10px;
    font-size: 12px;
    line-height: 26px;
  }
  .el-card__body{
    padding: 0;
  }
}
.query-select{
  margin-right: 20px;
}
.select-goods-dialog{
  .footer{
    margin-top: 30px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
